<template>
	<BaseContainer>
		<aplid-base-crud ref="crud" class="w-100" v-bind="baseConfig" :paramsObj="{
      ...paramsObj,
      countYears,
      type:1
    }" :operateFormate="handleOperate" :beforeOnLoadFormate="beforeOnLoadFormate">
      <template v-slot:pageTitle="{ total }" v-if="RegionName">
				<span class="text-large">{{ RegionName }}内机构（{{ total }}家）</span>
			</template>
			<template v-slot:menuRight>
				<div class="flex align-center mr8">
					<span class="text-bold">统计年月：</span>
					<el-date-picker
						v-model="countYears"
						:editable="false"
						:clearable="false"
						type="month"
						placeholder="选择统计年月"
						value-format="YYYY-MM"
						@change="handleRefresh"
					>
					</el-date-picker>
				</div>
			</template>
      <template v-slot:noticeChangeCompleteRate="{ row }">
				<span>{{ row.noticeChangeCompleteRate.toFixed(0) }}%</span>
			</template>
		</aplid-base-crud>

		<aplid-dialog
			@close="onClose"
			width="85%"
			v-model="visibleForm"
			append-to-body
			whickComm="Archives"
			:customeHeader="customeHeader"
			:model="{ ...nowRowData, ...detailHeadData }"
		>
			<transition name="fade" mode="out-in">
				<div class="dialog-content w-100" v-show="!Loading" v-if="visibleForm">
					<orgArchives :nowRowData="nowRowData" :showBtn="false" />
				</div>
			</transition>
			<PageLoading v-if="Loading" />
		</aplid-dialog>
	</BaseContainer>
</template>

<script setup lang="ts">
import apiMod from '@/apiMod/index'
import { baseConfig } from './config'
import orgArchives from '../../basicData/serviceOrg/components/Archives/index.vue'
import { useCommonStore } from '@/stores/common'
import { getStore } from '@/utils/store'
import { getAreaObj } from '@/utils/util'

const { setTempParams, setGloablParams, setMoreSearch } = useCommonStore()
const { Loading, detailHeadData } = storeToRefs(useCommonStore())

const getNowDate = () => {
	const y = new Date().getFullYear()
	const m = new Date().getMonth() +1 ||1
	return `${y}-${m >= 10 ? m : '0' + m}`
}

const crud = ref(null)
const countYears = ref(getNowDate())
const visibleForm = ref(false)
const paramsObj = ref({})
const userInfo = getStore('userInfo') || {}

const RegionName = ref('')

let areaObj = {
	provinceId: undefined,
	cityId: undefined,
	areaId: undefined,
	streetId: undefined,
	communityId: undefined,
}
if (userInfo.authProvince) areaObj.provinceId = userInfo.authProvince
if (userInfo.authCity) areaObj.cityId = userInfo.authCity
if (userInfo.authArea) areaObj.areaId = userInfo.authArea
if (userInfo.authStreet) areaObj.streetId = userInfo.authStreet
if (userInfo.authCommunity) areaObj.communityId = userInfo.authCommunity
paramsObj.value = {
	...areaObj,
}

const customeHeader = ref({
	type: 'CardOne',
	title: 'organizationName',
	avatar: 'organizationLogo',
	tipList: [{ prop: 'operateStatusLabel', type: 'tip-success', label: '运营状态' }],
	tagList: [
		{ prop: 'organizationTypeLabel', custome: 'tag', type: 'success', label: '机构类型' },
		{
			prop: 'integrity',
			type: 'success',
			label: '材料完整度',
			style: {
				background: '#1e80ff',
				padding: '2px 4px',
				borderRadius: '5px',
				fontWeight: 600,
			},
		},
	],
	menuBtn: [],
})

const nowRowData = ref({})
const onClose = () => {
	setTempParams({})
	setGloablParams({})
	setMoreSearch({})
}

const getServiceOrgInfo = (creditCode) => {
	return new Promise((resolve, reject) => {
		if (!creditCode) {
			reject()
		}
		apiMod.view_dpServiceOrgBase_creditCode(creditCode).then((res) => {
			resolve(res.data)
		})
	})
}
const handleRefresh = (e) => {
	nextTick(() => {
		crud.value?.refreshChange(
			{
				countYears: countYears.value,
				...paramsObj.value,
			},
			false
		)
	})
}

const beforeOnLoadFormate = (params)=> {
  const areasObj: any = getAreaObj(params.areas, '')
		return Promise.resolve({
			...params,
			...areasObj,
      countYears: countYears.value,
			areas: undefined,
		})
}
const handleOperate = (row, type) => {
	return new Promise(async (resolve, reject) => {
		if (type == 'refresh') {
			resolve(row)
			return
		}
		const data = await getServiceOrgInfo(row.creditCode) as any
		nowRowData.value = {
			...data,
		}
		visibleForm.value = true
	})
}

onMounted(() => {
	if (!userInfo.authStreet) return
	apiMod.admin_sysadministration_detail(userInfo.authStreet).then((res) => {
		RegionName.value = res.data.administrationName
	})
})

</script>

<style lang="scss" scoped></style>
